{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d25b48d6-51b9-4a8a-a780-71e70f8d0495",
   "metadata": {},
   "source": [
    "### 自动求导机制与梯度计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08be69db-9716-424b-99cc-5f9eff9435c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "# 创建需要跟踪梯度的张量\n",
    "x = torch.tensor(2.0, requires_grad=True)  # 创建一个标量张量x，值为2.0，并启用梯度跟踪\n",
    "w = torch.tensor(1.0, requires_grad=True)  # 创建一个标量张量w，值为1.0，并启用梯度跟踪\n",
    "b = torch.tensor(0.5, requires_grad=True)  # 创建一个标量张量b，值为0.5，并启用梯度跟踪"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc4e3e55-7162-4e76-b98b-e3e6363fbf0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "x,w,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f739696e-42ec-47ca-87e9-8e9ae5024251",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 前向计算\n",
    "y = w * x + b  # 构建计算图，y = w * x + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81ce8329-308f-4cba-ae89-4a02dc0a7710",
   "metadata": {},
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05b5d75d-3471-4568-b1e3-76b624c9f05e",
   "metadata": {},
   "source": [
    "### 梯度计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "192ceff1-a4c0-4ffe-8df4-9e026882c32f",
   "metadata": {},
   "outputs": [],
   "source": [
    "y.backward()  # 自动计算梯度，反向传播计算y关于x、w、b的梯度 y = w * x + b\n",
    "\n",
    "print(f'dy/dw = {w.grad}')  # 输出w的梯度，即dy/dw = x = 2.0\n",
    "print(f'dy/dx = {x.grad}')  # 输出x的梯度，即dy/dx = w = 1.0\n",
    "print(f'dy/db = {b.grad}')  # 输出b的梯度，即dy/db = 1.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f695ee49-6f48-4cda-8c0f-723825764f10",
   "metadata": {},
   "outputs": [],
   "source": [
    "y,x,w,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eb4c7acf-93e5-4bb2-af62-424c1abfa0ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 梯度累积特性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56386ca4-700d-4927-8d97-e3ef14fc0e8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 多次反向传播前需清零梯度，避免梯度累加\n",
    "x.grad.data.zero_()  # 清零x的梯度\n",
    "w.grad.data.zero_()  # 清零w的梯度\n",
    "b.grad.data.zero_()  # 清零b的梯度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "318b6ce5-7bb4-4ebd-b147-1fc5094894a1",
   "metadata": {},
   "source": [
    "## 简单示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9585d2f6-43b3-4da3-bf1c-1fae1fba324b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在x=2处的导数：49.00\n"
     ]
    }
   ],
   "source": [
    "# 多项式函数求导\n",
    "# f(x) = 3x³+2x²+5x+1\n",
    "x = torch.tensor(2.0, requires_grad=True)  # 创建一个标量张量x，值为2.0，并启用梯度跟踪\n",
    "f = 3*x**3 + 2*x**2 + 5*x + 1  # 定义多项式函数f(x)\n",
    "\n",
    "f.backward()  # 自动计算f关于x的梯度\n",
    "print(f'在x=2处的导数：{x.grad:.2f}')  # 输出x=2处的导数，应输出49.00"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dba250eb-cffc-4758-80fa-a821dcbe7a7b",
   "metadata": {},
   "source": [
    "## 高阶梯度应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "45b54a2b-2c2c-4330-8dbd-657029079bb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "二阶导数值：2.0\n"
     ]
    }
   ],
   "source": [
    "# 二阶倒数的应用\n",
    "x = torch.tensor(3.0, requires_grad=True)  # 创建一个标量张量x，值为3.0，并启用梯度跟踪\n",
    "y = x**2 + 2*x  # 定义函数y = x^2 + 2x\n",
    "\n",
    "# 一阶导\n",
    "first_grad = torch.autograd.grad(y, x, create_graph=True)[0]  # 计算y关于x的一阶导数，并保留计算图以便计算二阶导数\n",
    "# 二阶导\n",
    "second_grad = torch.autograd.grad(first_grad, x)[0]  # 计算一阶导数关于x的导数，即二阶导数\n",
    "\n",
    "print(f'二阶导数值：{second_grad}')  # 输出二阶导数值，应输出2.0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
